package com.customization.assets.formmode.sync;

import java.util.*;
import java.util.regex.Pattern;

import com.customization.assets.util.AbstractCronJob;
import com.customization.assets.util.DBTools;
import com.customization.assets.util.DateTools;

import com.customization.commons.LogTool;
import jxl.read.biff.Record;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.GCONST;
import weaver.general.Util;

/**
 * @author	Huangwm
 * @description	通过读取数据源的方式同步系统资产数据
 */
public class SyncAssetsInfoToEcology extends AbstractCronJob {

	private LogTool log = new LogTool("/log/assetlog/huangwm/SyncAssetsInfoToEcology",false);
	private DateTools dateTools = new DateTools();
	private DBTools dbTools = new DBTools();
	public String modeid = "";
	public String getFormmodeid(){
		return modeid;
	}
	public void setFormmodeid(String modeid){
		this.modeid = modeid;
	}
	public String dataSourceName = "";
	public String getdataSourceName(){
		return dataSourceName;
	}
	public void setdataSourceName(String dataSourceName){
		this.dataSourceName = dataSourceName;
	}

	public Pattern pattern = Pattern.compile("[0-9]*");

    public static void main(String[] args) {
        String rootPath = "D:\\WEAVER\\ecology\\";
        GCONST.setRootPath(rootPath);
        GCONST.setServerName("ecology");

        SyncAssetsInfoToEcology action = new SyncAssetsInfoToEcology();
		action.setdataSourceName("zongshudongtai");
		action.setFormmodeid("101");
        action.sync();
    }

	@Override
	public void start() throws  Exception{
        log.writeLog("本地读取数据源同步系统资产数据开始->>>" + dateTools.getCurrentTimes());
        log.writeLog("获取formmodeid参数 " + this.modeid);
		sync();
		updateGb();
		RecordSet rs = new RecordSet();
		rs.execute("update uf_gbxtzczmxx set cqzk=0 where kph not like '57S%' and  kph not like '57J%'");//更新产权状况为行政单位组成
		rs.execute("update uf_gbxtzczmxx set cqzk=1 where kph  like '57S%' or  kph  like '57J%'");//更新产权状况为事业单位组成
        log.writeLog("本地读取数据源同步系统资产数据结束->>>" + dateTools.getCurrentTimes());
		RecordSet rs1 = new RecordSet();
		rs1.execute("update uf_swzczmxx set synx = DATEDIFF(YEAR,rkrq,getdate())+1 where xzgbxtzc is null ");

		RecordSet rs2 = new RecordSet();
		rs2.execute("update uf_gbxtzczmxx set synx=DATEDIFF(YEAR,qdrq,getdate())+1 ");
        rs2.execute("update uf_swzczmxx set kph=uf_gbxtzczmxx.kph,gbxtzcbm2=uf_gbxtzczmxx.sybm,gbzcpp=uf_gbxtzczmxx.pp,gbzcggxh=uf_gbxtzczmxx.ggxh,synx=uf_gbxtzczmxx.synx \n" +
                "from uf_gbxtzczmxx where xzgbxtzc=uf_gbxtzczmxx.kpwybs");//更新实物资产中已经关联了关保资产的关保资产相关信息
        rs2.execute("update uf_uf_gbxtzczmxxexc set kph=uf_gbxtzczmxx.kph, pp=uf_gbxtzczmxx.pp,ggxh=uf_gbxtzczmxx.ggxh\n" +
                "from uf_gbxtzczmxx where gbzcid=uf_gbxtzczmxx.kpwybs");//更新帐实对碰关保盘亏表中关保资产相关信息

	}
	public void sync(){
		//读取数据源 zongshudongtai 里的视图 AssetsToEcology
		List<Map<String, String>> allDatasFirst = getAllAssetsInfoToDataSource(dataSourceName);
		List<String> assetsKpwybs = getAssetsKpwybsFromEcology();
		//List<Map<String, String>> allDatasSecond = getAllAssetsInfoToDataSourceGetSecond(dataSourceName);
		int formmodeid = Integer.parseInt(this.modeid);//正式系统为101 测试系统为102
		int modedatacreater = 1;
		int modedatacreatertype = 0;
		String modedatacreatedate = dateTools.getCurrentDate();
		String modedatacreatetime = dateTools.getCurrentTime();

		String insertSql = "insert into uf_gbxtzczmxx(formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,MODEUUID,";
		insertSql += "uid1,kpwybs,kph,mldm,mlmc,gdzcbm,gdzcmc,ggxh,pp,dj,zcly,kpzt,sxrq,qdrq,jssj,syr,sybm,cfdd,bz,sybm2,dwmc,hth)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

		String updateSql = "update uf_gbxtzczmxx set kph=?,mldm=?,mlmc=?,gdzcbm=?,gdzcmc=?,ggxh=?,pp=?,dj=?,zcly=?,kpzt=?,sxrq=?,qdrq=?,jssj=?,syr=?,sybm=?,cfdd=?,bz=?,sybm2=?,dwmc=?,modedatamodifydatetime=? where kpwybs=?";

		RecordSet rs = new RecordSet();
		RecordSet check = new RecordSet();
		RecordSetDataSource dataSource = null ;

		//建模表中无数据
		if (assetsKpwybs.size() <= 0) {
			log.writeLog("sync-->>建模表中无数据需要将所有数据插入");
			int insertCount = 0;
			for (Map<String, String> data : allDatasFirst) {
				//while(iterator.hasNext()){
				//获取到本次循环里的数据
				//Map<String, String> data = iterator.next();
				String id = data.get("id");
				String kpwybs = data.get("kpwybs");
				String kph = data.get("kph");
				String mldm = data.get("mldm");
				String mlmc = data.get("mlmc");
				String gdzcbm = data.get("gdzcbm");
				String gdzcmc = data.get("gdzcmc");
				String ggxh = data.get("ggxh");
				String pp = data.get("pp");
				String dj = data.get("dj");
				String zcly = data.get("zcly");
				String kpzt = data.get("kpzt");
				String sxrq = data.get("sxrq");
				String qdrq = data.get("qdrq");
				String jssj = data.get("jssj");
				String syr = data.get("syr");
				//String userid = dbTools.getUserid(syr);
				String sybm = data.get("sybm");
				String dwmc = data.get("dwmc");
				//String depid = dbTools.getDepid(userid);
				String cfdd = data.get("cfdd");
				String bz = data.get("bz");
				String hth = data.get("hth");
				//zcly kpzt sybm 需要根据选择框进行匹配值
				//插入的sql参数
				Object[] insertParams = {
						formmodeid, modedatacreater, modedatacreatertype, modedatacreatedate, modedatacreatetime, dbTools.getUUID(),
						id, kpwybs, kph, mldm, dbTools.getGbzclxByMldm(mldm), gdzcbm, gdzcmc, ggxh, pp, dj, dbTools.getObjToZcly(zcly), dbTools.getObjToKpzt(kpzt), sxrq, qdrq, jssj, syr, getSubcompanyid(sybm, dwmc,kph), cfdd, bz,sybm,dwmc,hth
				};

				//先判断下卡片唯一标识是否存在关保资产台账表里,如存在则更新这条数据
				String checkKpwybs = "select id,kpwybs,kph,sxrq from uf_gbxtzczmxx where kpwybs='"+kpwybs+"'";
				check.execute(checkKpwybs);
				if(check.next()){
					Map<String,String> getAssetInfo = getAssetsInfoToDataSourceCondition(dataSourceName,kpwybs,dataSource);
					String sxrq_update = getAssetInfo.get("sxrq");
					String kph_update = getAssetInfo.get("kph");
					String mldm_update = getAssetInfo.get("mldm");
					String mlmc_update = getAssetInfo.get("mlmc");
					String gdzcbm_update = getAssetInfo.get("gdzcbm");
					String gdzcmc_update = getAssetInfo.get("gdzcmc");
					String ggxh_update = getAssetInfo.get("ggxh");
					String pp_update = getAssetInfo.get("pp");
					String dj_update = getAssetInfo.get("dj");
					String zcly_update = getAssetInfo.get("zcly");
					String kpzt_update = getAssetInfo.get("kpzt");
					String qdrq_update = getAssetInfo.get("qdrq");
					String jssj_update = getAssetInfo.get("jssj");
					String syr_update = getAssetInfo.get("syr");
					String sybm_update = getAssetInfo.get("sybm");
					String dwmc_update = getAssetInfo.get("dwmc");
					String cfdd_update = getAssetInfo.get("cfdd");
					String bz_update = getAssetInfo.get("bz");
					Object[] updaetParams = {
							kph_update, mldm_update, dbTools.getGbzclxByMldm(mldm_update), gdzcbm_update, gdzcmc_update, ggxh_update, pp_update, dj_update, dbTools.getObjToZcly(zcly_update), dbTools.getObjToKpzt(kpzt_update), sxrq_update, qdrq_update, jssj_update, syr_update, getSubcompanyid(sybm_update, dwmc_update,kph), cfdd_update, bz_update,sybm_update,dwmc_update,dateTools.getCurrentTimes(), kpwybs
					};
					try {
						boolean isUpdate = rs.executeUpdate(updateSql, updaetParams);
						if (isUpdate) {
							log.writeLog(Arrays.asList(updaetParams));
							log.writeLog("sync-->>卡片唯一标识["+kpwybs+"]的数据已更新为最新");
						}
					} catch (Exception e) {
						log.writeLog(e.getMessage());
						e.printStackTrace();
					}
					/*
					String thisSxrq = check.getString("sxrq");
					if(dateTools.compare_date(sxrq, thisSxrq)){
						log.writeLog("卡片唯一标识已经插入过,且当前获取的数据生效日期大于数据库中的生效日期,进行更新");
						log.writeLog(checkKpwybs);
						log.writeLog("表中数据："+check.getString("kpwybs")+">"+check.getString("kph")+">"+thisSxrq+">id "+check.getString("id"));
						log.writeLog("当前参数："+kpwybs+">"+kph+">"+sxrq);
						try {
							boolean isUpdate = rs.executeUpdate(updateSql, updaetParams);
							if (isUpdate) {
								log.writeLog(Arrays.asList(updaetParams));
								log.writeLog("该卡片唯一标识的数据已更新为最新");
							}
						} catch (Exception e) {
							log.writeLog(e.getMessage());
							e.printStackTrace();
						}
					}
					 */
				}else{
					try {
						boolean isInsert = rs.executeUpdate(insertSql, insertParams);
						if (isInsert) {
							//log.writeLog("uid["+id+"]的数据已插入到表中");
							insertCount++;
						}
					} catch (Exception e) {
						log.writeLog(e.getMessage());
						e.printStackTrace();
					}
				}
			}
			log.writeLog(" sync-->>插入的总数  " + insertCount);
			if (insertCount > 0) {
				log.writeLog("开始进行权限重构=>>>" + dateTools.getCurrentTimes());
				int maxId = 0;
				RecordSet rs2 = new RecordSet();
				String sqlSearch = "select max(id) as mid from uf_gbxtzczmxx";
				rs2.execute(sqlSearch);
				if (rs2.next()) {
					maxId = rs2.getInt("mid");
				}
				log.writeLog("插入 uf_gbxtzczmxx 表的最大数据id： " + maxId);
				//表单建模权限重构
				for (int i = maxId; i > maxId - insertCount; i--) {
					refreshRight(modedatacreater, formmodeid, i);
				}
				log.writeLog("权限重构结束=>>>" + dateTools.getCurrentTimes());
			}
		} else {
			log.writeLog("sync-->>建模表中有数据进行更新");
			//遍历获取到的关保系统资产数据
			Iterator<Map<String, String>> iterator = allDatasFirst.iterator();
			int insertCount = 0;//用于记录插入了多少条数据
			int updateCount = 0;//用于记录更新了多少条数据
			for (Map<String, String> data : allDatasFirst) {
				//获取到本次循环里的数据
				String id = data.get("id");
                String kpwybs = data.get("kpwybs");
				String sxrq = data.get("sxrq");
				String kph = data.get("kph");
				String mldm = data.get("mldm");
				String mlmc = data.get("mlmc");
				String gdzcbm = data.get("gdzcbm");
				String gdzcmc = data.get("gdzcmc");
				String ggxh = data.get("ggxh");
				String pp = data.get("pp");
				String dj = data.get("dj");
				String zcly = data.get("zcly");
				String kpzt = data.get("kpzt");
				String qdrq = data.get("qdrq");
				String jssj = data.get("jssj");
				String syr = data.get("syr");
				String sybm = data.get("sybm");
				String dwmc = data.get("dwmc");
				String cfdd = data.get("cfdd");
				String bz = data.get("bz");
				String hth = data.get("hth");
				Object[] insertParams = {
						formmodeid, modedatacreater, modedatacreatertype, modedatacreatedate, modedatacreatetime, dbTools.getUUID(), id,
						kpwybs, kph, mldm, dbTools.getGbzclxByMldm(mldm), gdzcbm, gdzcmc, ggxh, pp, dj, dbTools.getObjToZcly(zcly), dbTools.getObjToKpzt(kpzt), sxrq, qdrq, jssj, syr, getSubcompanyid(sybm, dwmc,kph), cfdd, bz, sybm, dwmc,hth
				};
                //先判断下卡片唯一标识是否存在关保资产台账表里,如存在则更新这条数据的标识为历史数据
				String checkKpwybs = "select id,kpwybs,kph,sxrq from uf_gbxtzczmxx where kpwybs='"+kpwybs+"'";
				check.execute(checkKpwybs);
				//如果关保资产表中有数据，则循环关保建模表唯一标识后再查询下数据源关保资产表
				if(check.next()){
					//for (String key :assetsKpwybs){
					//Map<String,String> getAssetInfo = getAssetsInfoToDataSourceCondition(dataSourceName,key,dataSource);
					Map<String,String> getAssetInfo = getAssetsInfoToDataSourceCondition(dataSourceName,kpwybs,dataSource);
					String sxrq_update = getAssetInfo.get("sxrq");
					String kph_update = getAssetInfo.get("kph");
					String mldm_update = getAssetInfo.get("mldm");
					String mlmc_update = getAssetInfo.get("mlmc");
					String gdzcbm_update = getAssetInfo.get("gdzcbm");
					String gdzcmc_update = getAssetInfo.get("gdzcmc");
					String ggxh_update = getAssetInfo.get("ggxh");
					String pp_update = getAssetInfo.get("pp");
					String dj_update = getAssetInfo.get("dj");
					String zcly_update = getAssetInfo.get("zcly");
					String kpzt_update = getAssetInfo.get("kpzt");
					String qdrq_update = getAssetInfo.get("qdrq");
					String jssj_update = getAssetInfo.get("jssj");
					String syr_update = getAssetInfo.get("syr");
					String sybm_update = getAssetInfo.get("sybm");
					String dwmc_update = getAssetInfo.get("dwmc");
					String cfdd_update = getAssetInfo.get("cfdd");
					String bz_update = getAssetInfo.get("bz");
					Object[] updaetParams = {
							kph_update, mldm_update, dbTools.getGbzclxByMldm(mldm_update), gdzcbm_update, gdzcmc_update, ggxh_update, pp_update, dj_update, dbTools.getObjToZcly(zcly_update), dbTools.getObjToKpzt(kpzt_update), sxrq_update, qdrq_update, jssj_update, syr_update, getSubcompanyid(sybm_update, dwmc_update,kph_update), cfdd_update, bz_update,sybm_update,dwmc_update,dateTools.getCurrentTimes(), kpwybs
					};
					try {
						boolean isUpdate = rs.executeUpdate(updateSql, updaetParams);
						if (isUpdate) {
							log.writeLog("sync-->>卡片唯一标识["+kpwybs+"]的数据已更新为最新");
							log.writeLog(Arrays.asList(updaetParams));
							updateCount++;
						}
					} catch (Exception e) {
						log.writeLog(e.getMessage());
						e.printStackTrace();
					}
					/*
					String thisSxrq = check.getString("sxrq");
					if(dateTools.compare_date(sxrq, thisSxrq)) {
						log.writeLog("卡片唯一标识已经插入过,且当前获取的数据生效日期大于数据库中的生效日期,进行更新");
						log.writeLog(checkKpwybs);
						log.writeLog("表中数据：" + check.getString("kpwybs") + ">" + check.getString("kph") + ">" + thisSxrq + ">id " + check.getString("id"));
						log.writeLog("当前参数："+kpwybs+">"+kph+">"+sxrq);
						try {
							boolean isUpdate = rs.executeUpdate(updateSql, updaetParams);
							if (isUpdate) {
								log.writeLog("该卡片唯一标识的数据已更新为最新");
								log.writeLog(Arrays.asList(updaetParams));
								updateCount++;
							}
						} catch (Exception e) {
							log.writeLog(e.getMessage());
							e.printStackTrace();
						}
					}
					 */
					//}
				} else {
					try {
						boolean isInsert = rs.executeUpdate(insertSql, insertParams);
						if (isInsert) {
							insertCount++;
						}
					} catch (Exception e) {
						log.writeLog(e.getMessage());
						e.printStackTrace();
					}
				}
			}
			log.writeLog(" 插入的总数  " + insertCount);
			log.writeLog(" 更新的总数  " + updateCount);
			if (insertCount > 0) {
				log.writeLog("开始进行权限重构=>>>" + dateTools.getCurrentTimes());
				int maxId = 0;
				RecordSet rs2 = new RecordSet();
				String sqlSearch = "select max(id) as mid from uf_gbxtzczmxx";
				rs2.execute(sqlSearch);
				if (rs2.next()) {
					maxId = rs2.getInt("mid");
				}
				log.writeLog("插入 uf_gbxtzczmxx 表的最大数据id： " + maxId);
				//表单建模权限重构
				for (int i = maxId; i > maxId - insertCount; i--) {
					refreshRight(modedatacreater, formmodeid, i);
				}
				log.writeLog("权限重构结束=>>>" + dateTools.getCurrentTimes());
			}
		}
	}

	/**
	 * 获取数据源zongshudongtai里的视图 AssetsToEcology全量数据 不含已减少 已调拨 已报损报废的数据
	 * 该方法在第一次同步使用
	 * @return
	 */
	public  List<Map<String, String>> getAllAssetsInfoToDataSource(String dataSourceName){
		ArrayList<Map<String, String>> lists = new ArrayList<Map<String, String>>();
		RecordSetDataSource rs = new RecordSetDataSource(dataSourceName);
		String sql = " select * from AssetsToEcology where ";
		       sql += "(mldm='103019999' and ((gdzcmc like '%工作站%' or gdzcmc like '%工作站%' or gdzcmc like '%平板电脑%')  ";
		       sql += " or (pp like '%工作站%' or pp like '%工作站%' or pp like '%平板电脑%')  ";
		       sql += " or (ggxh like '%工作站%' or ggxh like '%工作站%' or ggxh like '%平板电脑%')) and  kpzt <>'已减少' and kpzt<>'已调拨' and kpzt<>'已报损报废' and kpzt<>'报损报废中') ";
		       sql += " or ( mldm<>'103019999' and kpzt <>'已减少' and kpzt<>'已调拨' and kpzt<>'已报损报废' and kpzt<>'报损报废中')  order by sxrq desc  ";
		rs.execute(sql);
		//log.writeLog(sql);
		int count = 0;
		while(rs.next()){
			Map<String, String> dataMap = new HashMap<String, String>();
			dataMap.put("id", Util.null2String(rs.getString("ID")));//卡片号
			dataMap.put("kph", Util.null2String(rs.getString("kph")));//卡片号
			dataMap.put("kpwybs", Util.null2String(rs.getString("kpwybs")));//卡片唯一标识
			dataMap.put("mldm", Util.null2String(rs.getString("mldm")));//目录代码
			dataMap.put("mlmc", Util.null2String(rs.getString("mlmc")));//目录名称
			dataMap.put("gdzcbm", Util.null2String(rs.getString("gdzcbm")));//固定资产编码
			dataMap.put("gdzcmc", Util.null2String(rs.getString("gdzcmc")));//固定资产名称
			dataMap.put("gjdm", Util.null2String(rs.getString("gjdm")));//国际代码
			dataMap.put("ggxh", Util.null2String(rs.getString("ggxh")));//规格型号
			dataMap.put("pp", Util.null2String(rs.getString("pp")));//品牌
			dataMap.put("zcly", Util.null2String(rs.getString("zcly")));//资产来源
			dataMap.put("qdfs", Util.null2String(rs.getString("qdfs")));//取得方式
			dataMap.put("cgzzxs", Util.null2String(rs.getString("cgzzxs")));//采购组织形式
			dataMap.put("dj", !pattern.matcher(Util.null2String(rs.getString("dj"))).matches()?"":rs.getString("dj"));//单价
			dataMap.put("sl", Util.null2String(rs.getString("sl")));//数量
			dataMap.put("je", Util.null2String(rs.getString("je")));//金额
			dataMap.put("kpzt", Util.null2String(rs.getString("kpzt")));//卡片状态
			dataMap.put("ccxh", Util.null2String(rs.getString("ccxh")));//出厂序号
			dataMap.put("hth", Util.null2String(rs.getString("hth")));//合同号
			dataMap.put("syfx", Util.null2String(rs.getString("syfx")));//使用方向
			dataMap.put("syr", Util.null2String(rs.getString("syr")));//使用人
			dataMap.put("sybm", Util.null2String(rs.getString("sybm")));//使用部门
			dataMap.put("cfdd", Util.null2String(rs.getString("cfdd")));//存放地点
			dataMap.put("dwmc", Util.null2String(rs.getString("dwmc")));//单位名称
			dataMap.put("sxrq", Util.null2String(rs.getString("sxrq")));//生效日期
			dataMap.put("qdrq", Util.null2String(rs.getString("qdrq")));//取得日期
			dataMap.put("trsyrq", Util.null2String(rs.getString("trsyrq")));//投入使用日期
			dataMap.put("jssj", Util.null2String(rs.getString("jssj")));//减少时间
			dataMap.put("zjdh", Util.null2String(rs.getString("zjdh")));//增加单号
			dataMap.put("yzcbm", Util.null2String(rs.getString("yzcbm")));//原资产编码
			dataMap.put("hth", Util.null2String(rs.getString("hth")));//合同号
			dataMap.put("bz", Util.null2String(rs.getString("bz")));//备注
			dataMap.put("remark1", Util.null2String(rs.getString("remark1")));//附加信息1
			dataMap.put("remark2", Util.null2String(rs.getString("remark2")));//附加信息2
			dataMap.put("remark3", Util.null2String(rs.getString("remark3")));//附加信息3
			dataMap.put("remark4", Util.null2String(rs.getString("remark4")));//附加信息4
			count ++;
			lists.add(dataMap);
		}
        log.writeLog("数据源视图 AssetsToEcology 首次获取全数量(不含报损报废、以减少、已调拨) >>>"+count);
		return lists;
	}

	/**
	 * 获取数据源zongshudongtai里的视图 AssetsToEcology全量数据 含已减少 已调拨 已报损报废的数据
	 * @return
	 */
	public  List<Map<String, String>> getAllAssetsInfoToDataSourceGetSecond(String dataSourceName){
		ArrayList<Map<String, String>> lists = new ArrayList<Map<String, String>>();
		RecordSetDataSource rs = new RecordSetDataSource(dataSourceName);
		String sql = "select * from AssetsToEcology where (mldm='103019999' and ((gdzcmc like '%工作站%' or gdzcmc like '%工作站%' or gdzcmc like '%平板电脑%') ";
		sql += " or (pp like '%工作站%' or pp like '%工作站%' or pp like '%平板电脑%') ";
		sql += " or (ggxh like '%工作站%' or ggxh like '%工作站%' or ggxh like '%平板电脑%')) and  (kpzt = '已减少' or kpzt='已调拨' or kpzt='已报损报废'  or kpzt='报损报废中')) ";
		sql += " or ( mldm<>'103019999' and (kpzt = '已减少' or kpzt='已调拨' or kpzt='已报损报废' or kpzt='报损报废中')) order by sxrq desc    ";
		rs.execute(sql);
		//log.writeLog(sql);
		int count = 0;
		while(rs.next()){
			Map<String, String> dataMap = new HashMap<String, String>();
			dataMap.put("id", Util.null2String(rs.getString("ID")));//卡片号
			dataMap.put("kph", Util.null2String(rs.getString("kph")));//卡片号
			dataMap.put("kpwybs", Util.null2String(rs.getString("kpwybs")));//卡片唯一标识
			dataMap.put("mldm", Util.null2String(rs.getString("mldm")));//目录代码
			dataMap.put("mlmc", Util.null2String(rs.getString("mlmc")));//目录名称
			dataMap.put("gdzcbm", Util.null2String(rs.getString("gdzcbm")));//固定资产编码
			dataMap.put("gdzcmc", Util.null2String(rs.getString("gdzcmc")));//固定资产名称
			dataMap.put("gjdm", Util.null2String(rs.getString("gjdm")));//国际代码
			dataMap.put("ggxh", Util.null2String(rs.getString("ggxh")));//规格型号
			dataMap.put("pp", Util.null2String(rs.getString("pp")));//品牌
			dataMap.put("zcly", Util.null2String(rs.getString("zcly")));//资产来源
			dataMap.put("qdfs", Util.null2String(rs.getString("qdfs")));//取得方式
			dataMap.put("cgzzxs", Util.null2String(rs.getString("cgzzxs")));//采购组织形式
			dataMap.put("dj", !pattern.matcher(Util.null2String(rs.getString("dj"))).matches()?"":rs.getString("dj"));//单价
			dataMap.put("sl", Util.null2String(rs.getString("sl")));//数量
			dataMap.put("je", Util.null2String(rs.getString("je")));//金额
			dataMap.put("kpzt", Util.null2String(rs.getString("kpzt")));//卡片状态
			dataMap.put("ccxh", Util.null2String(rs.getString("ccxh")));//出厂序号
			dataMap.put("hth", Util.null2String(rs.getString("hth")));//合同号
			dataMap.put("syfx", Util.null2String(rs.getString("syfx")));//使用方向
			dataMap.put("syr", Util.null2String(rs.getString("syr")));//使用人
			dataMap.put("sybm", Util.null2String(rs.getString("sybm")));//使用部门
			dataMap.put("cfdd", Util.null2String(rs.getString("cfdd")));//存放地点
			dataMap.put("dwmc", Util.null2String(rs.getString("dwmc")));//单位名称
			dataMap.put("sxrq", Util.null2String(rs.getString("sxrq")));//生效日期
			dataMap.put("qdrq", Util.null2String(rs.getString("qdrq")));//取得日期
			dataMap.put("trsyrq", Util.null2String(rs.getString("trsyrq")));//投入使用日期
			dataMap.put("jssj", Util.null2String(rs.getString("jssj")));//减少时间
			dataMap.put("zjdh", Util.null2String(rs.getString("zjdh")));//增加单号
			dataMap.put("yzcbm", Util.null2String(rs.getString("yzcbm")));//原资产编码
			dataMap.put("hth", Util.null2String(rs.getString("hth")));//合同号
			dataMap.put("bz", Util.null2String(rs.getString("bz")));//备注
			dataMap.put("remark1", Util.null2String(rs.getString("remark1")));//附加信息1
			dataMap.put("remark2", Util.null2String(rs.getString("remark2")));//附加信息2
			dataMap.put("remark3", Util.null2String(rs.getString("remark3")));//附加信息3
			dataMap.put("remark4", Util.null2String(rs.getString("remark4")));//附加信息4
			count ++;
			lists.add(dataMap);
		}
		log.writeLog("数据源视图 AssetsToEcology 获取带有报损报废、已减少、已调拨等状态数据 >>>"+count);
		return lists;
	}

	/**
	 * 负责更新在关保系统已报损报废但是同步时由于不读取这几个状态的数据导致在同步方法中无法更新的数据
	 */
	public void updateGb(){
		log.writeLog("-----更新在关保系统已报损报废但是同步时由于不读取这几个状态的数据导致在同步方法中无法更新的数据Start-----");
		List<Map<String, String>> allDatasSecond = getAllAssetsInfoToDataSourceGetSecond(dataSourceName);
		List<String> assetsKpwybs = getAssetsKpwybsFromEcology();
		RecordSetDataSource dataSource = null ;
		RecordSet rs = new RecordSet();
		String updateSql = "update uf_gbxtzczmxx set kph=?,mldm=?,mlmc=?,gdzcbm=?,gdzcmc=?,ggxh=?,pp=?,dj=?,zcly=?,kpzt=?,sxrq=?,qdrq=?,jssj=?,syr=?,sybm=?,cfdd=?,bz=?,sybm2=?,dwmc=?,modedatamodifydatetime=? where kpwybs=?";
		int updateCount = 0;

		for(Map<String,String> data : allDatasSecond ){
			//获取到本次循环里的数据
			String id = data.get("id");
			String kpwybs = data.get("kpwybs");
			String sxrq = data.get("sxrq");
			String kph = data.get("kph");
			String mldm = data.get("mldm");
			String mlmc = data.get("mlmc");
			String gdzcbm = data.get("gdzcbm");
			String gdzcmc = data.get("gdzcmc");
			String ggxh = data.get("ggxh");
			String pp = data.get("pp");
			String dj = data.get("dj");
			String zcly = data.get("zcly");
			String kpzt = data.get("kpzt");
			String qdrq = data.get("qdrq");
			String jssj = data.get("jssj");
			String syr = data.get("syr");
			String sybm = data.get("sybm");
			String dwmc = data.get("dwmc");
			String cfdd = data.get("cfdd");
			String bz = data.get("bz");
			String hth = data.get("hth");
			if(assetsKpwybs.contains(kpwybs)){
				log.writeLog("updateGb-->>该卡片唯一标识["+kpwybs+"]在关保资产表中存在,进行更新");
				Map<String,String> getAssetInfo = getAssetsInfoToDataSourceCondition(dataSourceName,kpwybs,dataSource);
				String sxrq_update = getAssetInfo.get("sxrq");
				String kph_update = getAssetInfo.get("kph");
				String mldm_update = getAssetInfo.get("mldm");
				String mlmc_update = getAssetInfo.get("mlmc");
				String gdzcbm_update = getAssetInfo.get("gdzcbm");
				String gdzcmc_update = getAssetInfo.get("gdzcmc");
				String ggxh_update = getAssetInfo.get("ggxh");
				String pp_update = getAssetInfo.get("pp");
				String dj_update = getAssetInfo.get("dj");
				String zcly_update = getAssetInfo.get("zcly");
				String kpzt_update = getAssetInfo.get("kpzt");
				String qdrq_update = getAssetInfo.get("qdrq");
				String jssj_update = getAssetInfo.get("jssj");
				String syr_update = getAssetInfo.get("syr");
				String sybm_update = getAssetInfo.get("sybm");
				String dwmc_update = getAssetInfo.get("dwmc");
				String cfdd_update = getAssetInfo.get("cfdd");
				String bz_update = getAssetInfo.get("bz");
				Object[] updaetParams = {
						kph_update, mldm_update, dbTools.getGbzclxByMldm(mldm_update), gdzcbm_update, gdzcmc_update, ggxh_update, pp_update, dj_update, dbTools.getObjToZcly(zcly_update), dbTools.getObjToKpzt(kpzt_update), sxrq_update, qdrq_update, jssj_update, syr_update, getSubcompanyid(sybm_update, dwmc_update,kph_update), cfdd_update, bz_update,sybm_update,dwmc_update,dateTools.getCurrentTimes(), kpwybs
				};
				try {
					boolean isUpdate = rs.executeUpdate(updateSql, updaetParams);
					if (isUpdate) {
						log.writeLog("updateGb-->>卡片唯一标识["+kpwybs+"]的数据已更新为最新");
						log.writeLog(Arrays.asList(updaetParams));
						updateCount++;
					}
				} catch (Exception e) {
					log.writeLog(e.getMessage());
					e.printStackTrace();
				}
			}
		}
		log.writeLog("更新"+updateCount+"条数据");
		log.writeLog("-----更新在关保系统已报损报废但是同步时由于不读取这几个状态的数据导致在同步方法中无法更新的数据End-----");
	}

	/**
	 * 根据卡片唯一标识获取数据源zongshudongtai里的视图信息
	 * @return
	 */
	public  Map<String, String> getAssetsInfoToDataSourceCondition(String dataSourceName,String condition,RecordSetDataSource rs){
		rs = new RecordSetDataSource(dataSourceName);
		Map<String, String> dataMap = new HashMap<String, String>();
		String sql = " select * from AssetsToEcology where kpwybs='"+condition+"' order by sxrq desc  ";
		rs.execute(sql);
		log.writeLog(sql);
		if(rs.first()){
			dataMap.put("id", Util.null2String(rs.getString("ID")));//卡片号
			dataMap.put("kph", Util.null2String(rs.getString("kph")));//卡片号
			dataMap.put("kpwybs", Util.null2String(rs.getString("kpwybs")));//卡片唯一标识
			dataMap.put("mldm", Util.null2String(rs.getString("mldm")));//目录代码
			dataMap.put("mlmc", Util.null2String(rs.getString("mlmc")));//目录名称
			dataMap.put("gdzcbm", Util.null2String(rs.getString("gdzcbm")));//固定资产编码
			dataMap.put("gdzcmc", Util.null2String(rs.getString("gdzcmc")));//固定资产名称
			dataMap.put("gjdm", Util.null2String(rs.getString("gjdm")));//国际代码
			dataMap.put("ggxh", Util.null2String(rs.getString("ggxh")));//规格型号
			dataMap.put("pp", Util.null2String(rs.getString("pp")));//品牌
			dataMap.put("zcly", Util.null2String(rs.getString("zcly")));//资产来源
			dataMap.put("qdfs", Util.null2String(rs.getString("qdfs")));//取得方式
			dataMap.put("cgzzxs", Util.null2String(rs.getString("cgzzxs")));//采购组织形式
			dataMap.put("dj", !pattern.matcher(Util.null2String(rs.getString("dj"))).matches()?"":rs.getString("dj"));//单价
			dataMap.put("sl", Util.null2String(rs.getString("sl")));//数量
			dataMap.put("je", Util.null2String(rs.getString("je")));//金额
			dataMap.put("kpzt", Util.null2String(rs.getString("kpzt")));//卡片状态
			dataMap.put("ccxh", Util.null2String(rs.getString("ccxh")));//出厂序号
			dataMap.put("hth", Util.null2String(rs.getString("hth")));//合同号
			dataMap.put("syfx", Util.null2String(rs.getString("syfx")));//使用方向
			dataMap.put("syr", Util.null2String(rs.getString("syr")));//使用人
			dataMap.put("sybm", Util.null2String(rs.getString("sybm")));//使用部门
			dataMap.put("cfdd", Util.null2String(rs.getString("cfdd")));//存放地点
			dataMap.put("dwmc", Util.null2String(rs.getString("dwmc")));//单位名称
			dataMap.put("sxrq", Util.null2String(rs.getString("sxrq")));//生效日期
			dataMap.put("qdrq", Util.null2String(rs.getString("qdrq")));//取得日期
			dataMap.put("trsyrq", Util.null2String(rs.getString("trsyrq")));//投入使用日期
			dataMap.put("jssj", Util.null2String(rs.getString("jssj")));//减少时间
			dataMap.put("zjdh", Util.null2String(rs.getString("zjdh")));//增加单号
			dataMap.put("yzcbm", Util.null2String(rs.getString("yzcbm")));//原资产编码
			dataMap.put("hth", Util.null2String(rs.getString("hth")));//合同号
			dataMap.put("bz", Util.null2String(rs.getString("bz")));//备注
			dataMap.put("remark1", Util.null2String(rs.getString("remark1")));//附加信息1
			dataMap.put("remark2", Util.null2String(rs.getString("remark2")));//附加信息2
			dataMap.put("remark3", Util.null2String(rs.getString("remark3")));//附加信息3
			dataMap.put("remark4", Util.null2String(rs.getString("remark4")));//附加信息4
		}
		return dataMap;
	}

    /**
     * 获取建模表单-关保系统资产账目信息 卡片唯一标识
     * @return
     */
    public  List<String> getAssetsKpwybsFromEcology(){
        ArrayList<String> lists = new ArrayList<String>();
        RecordSet rs = new RecordSet();
        String sql = "select kpwybs from uf_gbxtzczmxx";
        rs.execute(sql);
        int count = 0;
        while(rs.next()){
            lists.add(Util.null2String(rs.getString("kpwybs")));
            count ++;
        }
        log.writeLog("获取的关保资产台账表卡片唯一编号数量 >>>"+count);
        return lists;
    }

	/**
	 * 表单建模权限重构方法，重构后才能在台账里查看到数据
	 * @param creater 创建人
	 * @param formmodeid 模块id
	 * @param billid 数据id,即对应uf表里的id
	 */
	  public void refreshRight( int creater, int formmodeid, int billid){
	    try{
	    	ModeRightInfo modeRightInfo = new ModeRightInfo();
	    	//console.info("当前重构数据：creater=" + creater + "  formmodeid=" + formmodeid + "  billid=" + billid);
	    	modeRightInfo.setNewRight(true);
	    	modeRightInfo.editModeDataShare(creater, formmodeid, billid);
	    	modeRightInfo.addDocShare(creater, formmodeid, billid);
	    }
	    catch (Exception e){
            log.writeLog("重构代码报错：creater=" + creater + "  formmodeid=" + formmodeid + "  billid=" + billid);
            log.writeLog(e.getMessage());
	    	e.printStackTrace();
	    }
	  }
	  
	  /**
	   * 根据使用部门和对应单位获取单位id
	   * @param sybm 资产数据源中的使用部门字段，可能是分部也可能是部门
	   * @param dydw 可能是分部或部门
	   * @return
	   */
	  public String getSubcompanyid(String sybm,String dydw,String kph){
		//先获取dydw进行判断,如果是拱北海关则取sybm，如果sybm包含办公室的则为系统中的"办公室（党委办公室）"，否则直接取分部表的数据
		//如果dydw属于中山海关，则判断下sybm最后一个字为‘处’则是分部表，或者办公室也为分部，如sybm中包含‘技术中心’则对应的是‘中山海关技术中心’，无法从分部表中找到sybm的则为赋值为中山海关
		//如果dydw属于缉私局，判断sybm是否以处或分局结尾，或者办公室则取分部表，无法从分部表中找到sybm的则为赋值为缉私局
		RecordSet rs1 = new RecordSet();
		log.writeLog("单位名称："+dydw+"  使用部门："+sybm+" 判断是否技术处"+sybm.indexOf("技术处"));
		if(!dydw.equals("")){
			if(dydw.equals("拱北海关")){
				log.writeLog("单位名称是拱北海关,使用部门是："+sybm+"  "+ sybm.indexOf("技术处") );
				String sql = "select * from HrmSubcompany where canceled<>1 and supsubcomid=1 and subcompanyname like '%"+sybm+"%'";
				if(!sybm.equals("")){
					if(sybm.indexOf("机关党委")>=0 || sybm.indexOf("工会")>=0){
						log.writeLog("使用部门包含机关党委/工会");
						return "35";
					}else if(sybm.indexOf("技术处")>=0){
                        log.writeLog("使用部门包含技术处 返回31");
                        return "31";
				    }else if(sybm.indexOf("监管通关处")>=0){
						log.writeLog("使用部门包含监管通关处 返回26");
						return "26";
					}else if(sybm.indexOf("关税处")>=0){
						log.writeLog("使用部门包含关税处 返回21");
						return "21";
					}else if(sybm.indexOf("综合统计处")>=0){
						log.writeLog("使用部门包含综合统计处 返回27");
						return "27";
					}else if(sybm.indexOf("稽查处")>=0){
						log.writeLog("使用部门包含稽查处 返回28");
						return "28";
					}else if(sybm.indexOf("风险防控")>=0){
						log.writeLog("使用部门包含风险防控 返回38");
						return "38";
					}else if(sybm.indexOf("离退休")>=0){
						log.writeLog("使用部门包含离退休干部办公室 返回37 ");
						return "37";
					}else if(sybm.indexOf("办公室")>=0){
						log.writeLog("使用部门包含办公室 返回17 ");
						return "17";
					}else if(sybm.indexOf("数据分中心")>=0){
						log.writeLog("使用部门包含数据分中心 返回42 ");
						return "42";
					}else if(sybm.indexOf("机关服务中心")>=0 || sybm.indexOf("物业管理")>=0 || sybm.indexOf("海关饭堂")>=0 || sybm.indexOf("医务所")>=0 || sybm.indexOf("幼儿园")>=0){
						log.writeLog("使用部门包含关服务中心或物业管理或海关饭堂或医务所或幼儿园 返回39 ");
						return "39";
					}else if(sybm.indexOf("珠海技术中心")>=0){
						log.writeLog("使用部门包含珠海技术中心 返回40 ");
						return "40";
					}else if(sybm.indexOf("珠海保健中心")>=0){
						log.writeLog("使用部门包含珠海保健中心 返回41 ");
						return "41";
					}else{
						rs1.execute(sql);
						if(rs1.next()){
							//log.writeLog("使用sql查询："+sql+" "+rs1.getString("id"));
							return rs1.getString("id");
						}
						log.writeLog("直接返回拱北海关的分部id");
						return "1";
					}
				}else{
					//log.writeLog("直接返回拱北海关的分部id");
					return "1";
				}			
			}else if(dydw.indexOf("中山海关")>=0){
				log.writeLog("进入中山海关的判断");
				if(dydw.equals("中山海关技术中心")){
                    log.writeLog("单位名称直接中山海关技术中心单独处理");
                    return "267";
                }
				String sql = "select * from HrmSubcompany where canceled<>1 and supsubcomid=5 and subcompanyname like '%"+sybm+"%'";
				if(!sybm.equals("")){
					if(sybm.indexOf("技术中心")>=0){
						log.writeLog("中山海关技术中心");
						return "267";
					}else if(sybm.indexOf("办公室")>=0){
						log.writeLog("中山海关办公室");
						return "264";
					}else{
						rs1.execute(sql);
						if(rs1.next()){
							//log.writeLog("使用sql查询："+sql+" "+rs1.getString("id"));
							return rs1.getString("id");
						}
						//log.writeLog("直接返回中山海关的分部id");
						return "5";
					}
				}else {
					//console.log("直接返回中山海关的分部id");
					return "5";
				}
			}else if(dydw.equals("缉私局")){
				log.writeLog("单位名称是缉私局");
				String sql = "select * from HrmSubcompany where canceled<>1 and supsubcomid=29 and subcompanyname like '%"+sybm+"%'";
				if(!sybm.equals("")){
					if(sybm.indexOf("办公室")>=0){
						log.writeLog("缉私局办公室");
						return "259";
					}else{
						rs1.execute(sql);
						if(rs1.next()){
							//console.log("使用sql查询："+sql+" "+rs1.getString("id"));
							return rs1.getString("id");
						}
						//console.log("直接返回缉私局的分部id");
						return "29";
					}
				}else{
					//console.log("直接返回缉私局的分部id");
					return "29";
				}
			}else{
				log.writeLog("单位不为空 且不在拱北海关、中山海关和缉私局内");
				if(dydw.indexOf("技术中心")>=0){
					return "40";
				}else if(dydw.indexOf("后勤管理中心")>=0 || dydw.indexOf("机关服务中心")>=0 ||  dydw.indexOf("物业管理")>=0 ||  dydw.indexOf("海关饭堂")>=0 || dydw.indexOf("医务所")>=0 || dydw.indexOf("幼儿园")>=0){
					return "39";
				}else if(dydw.indexOf("珠海保健中心")>=0){
					return "41";
				}else if(dydw.indexOf("驻香洲办事处")>=0){
                    return "13";
                }else if(dydw.indexOf("数据分中心")>=0){
					return "42";
				}else if(dydw.indexOf("珠海技术中心")>=0){
					return "40";
				}else if(dydw.indexOf("珠海保健中心")>=0){
					return "41";
				}else{
					String sql = "select * from HrmSubcompany where canceled<>1  and subcompanyname like '%"+dydw+"%' ";
					rs1.execute(sql);
					if(rs1.next()){
						//log.writeLog("需要直接用sql查询："+sql+" "+rs1.getString("id"));
						return rs1.getString("id");
					}
					log.writeLog(sql+"<<<查询不到 返回空");
					return "";
				}
			}
		}else{
			log.writeLog("单位名称为空 先查询卡片号的前4位 "+kph);
			if(kph.startsWith("5760")){
				log.writeLog("单位为空 判断卡片号以5760开头返回高栏海关");
				return "6";
			}else if(kph.startsWith("5750")){
				log.writeLog("单位为空 判断卡片号以5750开头返回九州海关");
				return "8";
			}else if(kph.startsWith("5720")){
				log.writeLog("单位为空 判断卡片号以5720开头返回中山海关");
				return "5";
			}else if(kph.startsWith("5740")){
				log.writeLog("单位为空 判断卡片号以5740开头返回湾仔海关");
				return "7";
			}else if(kph.startsWith("5770")){
				log.writeLog("单位为空 判断卡片号以5770开头返回斗门海关");
				return "15";
			}else if(kph.startsWith("5795")){
				log.writeLog("单位为空 判断卡片号以5795开头返回横琴海关");
				return "14";
			}else if(kph.startsWith("57S2")){
				log.writeLog("单位为空 判断卡片号以57S2开头返回后勤管理中心");
				return "39";
			}else if(kph.startsWith("57S8")){
				log.writeLog("单位为空 判断卡片号以57S8开头返回技术中心");
				return "40";
			}else if(kph.startsWith("57S9")){
				log.writeLog("单位为空 判断卡片号以57S9开头返回珠海保健中心");
				return "41";
			}else if(kph.startsWith("57S1")){
				log.writeLog("单位为空 判断卡片号以57S1开头返回拱北数据分中心");
				return "42";
			}else{
				log.writeLog("单位名称为空 查询使用部门");
				if(sybm.indexOf("机关党委")>=0){
					log.writeLog("使用部门包含机关党委");
					return "35";
				}else if(sybm.indexOf("关税处")>=0){
					log.writeLog("使用部门包含关税处");
					return "21";
				}else if(sybm.startsWith("技术处")){
					log.writeLog("使用部门包含技术处 返回31");
					return "31";
				}else if(sybm.indexOf("监管通关处")>=0){
					log.writeLog("使用部门包含监管通关处 返回26");
					return "26";
				}else if(sybm.indexOf("综合统计处")>=0){
					log.writeLog("使用部门包含综合统计处 返回27");
					return "27";
				}else if(sybm.indexOf("稽查处")>=0){
					log.writeLog("使用部门包含稽查处 返回28");
					return "28";
				}else if(sybm.indexOf("风险防控")>=0){
					log.writeLog("使用部门包含风险防控 返回38");
					return "38";
				}else if(sybm.indexOf("后勤管理中心")>=0 || sybm.indexOf("机关服务中心")>=0 || sybm.indexOf("物业管理")>=0 || sybm.indexOf("海关饭堂")>=0 || sybm.indexOf("医务所")>=0 || sybm.indexOf("幼儿园")>=0){
					log.writeLog("使用部门包含后勤管理中心或者机关服务中心或物业管理或海关饭堂 返回39");
					return "39";
				}else if(sybm.indexOf("离退休")>=0){
					log.writeLog("使用部门为离退休干部办公室 返回37 ");
					return "37";
				}else if(sybm.indexOf("办公室")>=0){
					log.writeLog("使用部门为办公室 返回17 ");
					return "17";
				}else if(sybm.indexOf("珠海技术中心")>=0){
					log.writeLog("使用部门包含珠海技术中心 返回40 ");
					return "40";
				}else if(sybm.indexOf("珠海保健中心")>=0){
					log.writeLog("使用部门包含珠海保健中心 返回41 ");
					return "41";
				}else{
					String sql = "select * from HrmSubcompany where canceled<>1  and subcompanyname like '%"+sybm+"%' ";
					rs1.execute(sql);
					if(rs1.next()){
						//log.writeLog("需要直接用sql查询："+sql+" "+rs1.getString("id"));
						return rs1.getString("id");
					}
					log.writeLog(sql+"<<<查询不到 返回空");
					return "";
				}
			}
		}
	 }
}

